package org.linxiangyu.juanorm;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.util.Log;

import java.io.*;


//TODO in android 4.2 , can'not open Database bug
//http://androiddev.orkitra.com/?p=13793


public enum JuanORM {
    INSTANCE;

    // 单例模式。JuanORM 管理整个App相关的Context，SQLiteDatase等对象
    Context context;
    String dbname;    //数据库
    File dbFile;
    String dbPath;
    public boolean isDebug = true;
    public String TAG = "JuanORM" ;


    private void initAppContext(Context context) {
        this.context = context;
        new File(context.getFilesDir().getAbsolutePath().replace("files", "databases") ).mkdirs();
    }

    private void initDBwithManifest() {
        try {
            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
            dbname = ai.metaData.get("JuanORM").toString();
            initDBwithName(dbname);
            if (isDebug)
                Log.d(TAG, dbname);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(TAG,
                    "JuanORM database configuration not found; Did you set properties in your app manifest? \n " +
                    "Example:\n" +
                    "        <meta-data\n" +
                    "                    android:name=\"JuanORM\"\n" +
                    "                   android:value=\"juan\"/>\n" +
                    "it will create a database name juan in your app"
                    );
            e.printStackTrace();
        }
    }

    private void initDBwithName(String dbname) {
        this.dbname = dbname;
        dbFile = context.getDatabasePath(dbname);
        dbPath = context.getFilesDir().getAbsolutePath().replace("files", "databases") + File.separator + dbname;

    }

    public void  initDBwithManifest(Context context) {
        initAppContext(context);
        initDBwithManifest();
    }

    public void initDBwithName(Context context, String dbname) {
        this.initAppContext(context);
        initDBwithName(dbname);
    }


    public void initDBwithPathAndName(Context context, String path, String name){
        this.initAppContext(context);
        dbFile = new File(path + name);
        dbname = name;
        dbPath = dbFile.getAbsolutePath();
    }



    public SQLiteDatabase getDatabase() {
        Log.d(TAG, dbPath) ;
        try {
            return SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE);
        } catch (SQLiteException e) {
            return  SQLiteDatabase.openOrCreateDatabase(dbPath, null);
        }
    }

    public void closeDataBase(){
        SQLiteDatabase db = getDatabase();
        if (db != null) {
            db.close();
        }
    }


    public void executeScript(SQLiteDatabase db, String file) {
        StringBuilder text = new StringBuilder();
        try {
            InputStream is = this.context.getAssets().open(file);

            BufferedReader r = new BufferedReader(new InputStreamReader(is));
            String line;
            while ((line = r.readLine()) != null) {
                text.append(line);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }


        Log.i("Sugar", "script : " + text.toString());
        db.execSQL(text.toString());
    }
}

